#ifndef SOCKCOMADT_H_
#define SOCKCOMADT_H_

/* Abstract data type */
typedef struct CommCDT * Comm;

/* Client/Server data types */
typedef enum {COMM_CLIENT, COMM_SERVER} commType;
typedef enum {SERVER, CLIENT, BOTH} sockType;


/**
 ** Initialize an instance of the ADT
 **
 ** @return			Returns an ADT
 **/
Comm newComm(void);

/**
 ** Prepare the sockets for the communication
 **
 ** @param	comm	ADT
 ** @param	type	Type of communication (client or server)
 ** @param	host	the host address
 ** @param	port	the host port
 ** @return			Returns a positive value if login was correctly done, or
 **					negative if an error ocurred.
 **/
int initComm(Comm comm, commType type, char * host, char * port);

/**
 ** Prepare the UDP socket for the communication
 **
 ** @param	comm	ADT
 ** @param	host	the host address
 ** @param	port	the host port
 ** @return			Returns a positive value if login was correctly done, or
 **					negative if an error ocurred.
 **/
int setUDP(Comm comm, char * host, char * port);

/**
 ** Close an UDP socket
 **
 ** @param	comm	ADT
 **/
void unsetUDP(Comm comm);

/**
 ** Sends a data stream to the remote host
 **
 ** @param	comm	ADT
 ** @param	p		data stream
 ** @param	qty		data stream length
 ** @param	service	service (TCP or UDP)
 **/
void send_packet(Comm comm, const void *p, int qty, int service );

/**
 ** Receives a data stream from the remote host
 **
 ** @param	comm	ADT
 ** @param	p		data stream
 ** @param	lim		max. data stream length
 ** @param	service	service (TCP or UDP)
 ** @return			Returns the bytes readed, or negative if an error ocurred.
 **/
int receive_packet(Comm comm, void *p, int lim, int service );

/**
 ** Receives a data stream from the remote host through UDP
 **
 ** @param	comm	ADT
 ** @param	p		data stream
 ** @param	lim		max. data stream length
 ** @return			Returns a positive value if its all ok, 
 **					or negative if an error ocurred.
 **/
int receive_UDP(Comm comm, void *p, int lim );

/**
 ** sends a data stream to the remote host through UDP
 **
 ** @param	comm	ADT
 ** @param	stream	data stream
 ** @param	lim		max. data stream length
 **/
void send_UDP(Comm comm, void * stream, int lim);

/**
 ** Closes communication
 **
 ** @param	comm	ADT
 ** @param	sock	Type of sock (master or slave)
 **/
void closeComm(Comm comm, sockType sock);

/**
 ** accepts a TCP conection request
 **
 ** @param	comm	ADT
 ** @return			Returns the socket number opened, 
 **					or negative if an error ocurred.
 **/
int acceptComm(Comm comm);

/**
 ** returns the IP corresponding to a certain connection
 **
 ** @param	comm	ADT
 ** @return			Returns a string containing the solicitated IP 
 **/
char * printIP(Comm comm);

/**
 ** Wrapper for select() function
 **
 ** @param	comm	ADT
 ** @return			Returns an int indicating which socket is available to read
 **/
int sockSel(Comm comm);

#endif /*SOCKCOMADT_H_*/
